<template>
  <span class="tools-li">
    <transition name="el-zoom-in-center">
      <div v-show="isShow" :key="tooltipId" ref="customAlert" class="custom-demo" transiton="el-zoom-in-center">
        <div v-if="isLogin === false" class="custom-body">
          <div class="custom-body-li-left"><i class="el-icon-switch-button" /></div>
          <div class="custom-body-li-right">
            <el-button type="text" size="medium">开始登陆</el-button>
          </div>
        </div>
        <div v-else class="custom-body">
          <div class="custom-body-li-left"><i class="el-icon-s-custom" /></div>
          <div class="custom-body-li-right">
            <el-button type="text" size="medium" @click="Logout">注销登录</el-button>
          </div>
        </div>
      </div>
    </transition>
    <slot name="reference" />
    <div v-if="systemList" class="custom-iframe">
      <iframe v-for="(item,index) in systemList" :key="'iframe-'+index" :src="item.url+item.logoutUrl" />
    </div>
  </span>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util';
import { getToken } from '@/utils/auth';
import Config from '@/settings';
import { mapGetters } from 'vuex';
import { queryAllSystem } from '@/api/system/usystem';
export default {
  name: 'PortalUserCommon',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      isShow: false,
      isLogin: false,
      systemList: []
    };
  },
  computed: {
    tooltipId() {
      return `el-popover-${generateId()}`;
    },
    ...mapGetters([
      'sidebar',
      'device',
      'user',
      'baseApi'
    ])
  },
  mounted() {
    if (getToken()) {
      this.isLogin = true;
    } else {
      this.isLogin = false;
    }
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose);
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose);
    }
  },
  methods: {
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    },
    triggerOpen() {
      this.isShow = true;
      this.$emit('callBack', this.isShow);
    },
    triggerClose() {
      this.isShow = false;
      this.$emit('callBack', this.isShow);
    },
    Logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.userLoginOut();
      }).catch(() => {
        this.$message.success('取消成功！');
      });
    },
    userLoginOut() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading'
      });
      queryAllSystem().then(res => {
        const { content } = res;
        this.systemList = content;
        this.$store.dispatch('LogOut').then(() => {
          if (Config.isCas) {
            setTimeout(() => {
              loading.close();
            }, 600);
            window.location.href = Config.casLogout + '/uc/user';
          } else {
            setTimeout(() => {
              loading.close();
            }, 600);
            location.reload();
          }
        }).catch(() => {
          if (Config.isCas) {
            setTimeout(() => {
              loading.close();
            }, 600);
            window.location.href = Config.casLogout + '/use/user';
          } else {
            setTimeout(() => {
              loading.close();
            }, 600);
            location.reload();
          }
        });
      }).catch(() => {
        this.$store.dispatch('LogOut').then(() => {
          if (Config.isCas) {
            loading.close();
            window.location.href = Config.casLogout + '/uc/user';
          } else {
            loading.close();
            location.reload();
          }
        }).catch(() => {
          if (Config.isCas) {
            loading.close();
            window.location.href = Config.casLogout + '/use/user';
          } else {
            loading.close();
            location.reload();
          }
        });
      });
    }
  }
};
</script>

<style scoped>
.custom-demo{
  position: absolute;
  width: 180px;
  left: -180px;
  height: 68px;
  top:0px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  background-color: #fff;
  color: #1f2d3d;
  font-size: 14px;
  font-weight: 400;
  border-radius: 4px;
}
.custom-body {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: row nowrap;
}
.custom-body-li-left {
  flex: 0 1 auto;
  width: 68px;
  font-size: 18px;
  font-weight: bolder;
  color: #00a0e9;
}
.custom-body-li-right {
  flex: 1 1 auto;
  text-align: left;
}
.custom-iframe {
  width: 100%;
  height: 0px;
  overflow: hidden;
}
</style>
